package com.tensquare.qa.controller;

import com.tensquare.qa.pojo.Comment;
import com.tensquare.qa.pojo.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.qa.service.CommentService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

import java.util.List;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", commentService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", commentService.findById(id));
    }


    /**
     * 根据问题id查询评论
     *
     * @param problemid 问题id
     * @return 分页结果
     */
    @RequestMapping(value = "/{problemid}", method = RequestMethod.POST)
    public Result findSearch(@PathVariable String problemid) {
        Page<Comment> pageList = commentService.findByProblemidAndReplyid(problemid, "0");
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Comment>(pageList.getTotalElements(), pageList.getContent()));
    }


    /**
     * 增加
     *
     * @param comment
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Comment comment) {
        commentService.add(comment);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param comment
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Comment comment, @PathVariable String id) {
        comment.set_id(id);
        commentService.update(comment);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        commentService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value = "/{problemid}/{replyid}", method = RequestMethod.GET)
    public Result findByProblemidAndReplyid(@PathVariable String problemid, @PathVariable String replyid) {
        Page<Comment> page = commentService.findByProblemidAndReplyid(problemid, replyid);
        System.out.println("问题id：" + problemid + "回答id：" + replyid);
        List<Comment> list = page.getContent();
        return new Result(true, StatusCode.OK, "查询成功！", list);
    }

}
